<?php defined('SYSPATH') OR die('No direct access allowed.');

class Hidden_Plugin extends WB_FormItem {
	
	var $changable = FALSE;
	var $function = NULL;
	
	public function __construct($name='', $params=array())
	{
		parent::__construct($name, $params);
		
		$this->changable = in_array('changable', $this->params, TRUE);
		$this->function = isset($this->params['function']) ? $this->params['function'] : NULL;
	}
	
	public function set_value($value, $type)
	{
		if ( ! empty($this->function))
		{
			$this->value = call_user_func_array(array(Kohana::instance(), $this->function), array($this->value, $type));
		}
		elseif ($type=='edit' && empty($this->changable)) $this->value = NULL;
		else parent::set_value($value, $type);
	}

	/**
	 * Renders the form element
	 * 
	 * @return string
	 */
	public function render_item()
	{
		if ($this->changable) return parent::render_item();
		
		return '<input type="hidden" name="'.$this->name.'" id="'.$this->name.'" value="'.$this->value.'" />';
	}
}

//End of Hidden_Plugin Library